package com.ruis.kotboot.common.util

import com.ruis.kotboot.common.core.CacheNames
import org.springframework.cache.Cache
import org.springframework.cache.CacheManager

object CacheUtil {
    private val cacheManager by lazy { SpringContextHolder.getBean(CacheManager::class.java) }

    fun getCache(name:String): Cache? {
        try{
            return cacheManager.getCache(name)
        }catch (e:Exception){}
        return null
    }

    fun getUtilCache(): Cache?{
        return getCache(CacheNames.RUIS_UTILCACHE)
    }

    fun cleanCache(name:String):Boolean{
        try{
            val cache=cacheManager.getCache(name)
            cache?.clear()
            return true
        }catch (e:Exception){}
        return false
    }
    fun cleanCache(name:String,key:String):Boolean{
        try{
            cacheManager.getCache(name)?.evict(key)
            return true
        }catch (e:Exception){}
        return false
    }
    fun cleanUtilCache(key:String):Boolean{
        try{
            getUtilCache()?.evict(key)
            return true
        }catch (e:Exception){}
        return false
    }


    fun cleanAllCache(){
        cleanCache(CacheNames.SYS_USER)
        cleanCache(CacheNames.SYS_PARAM)
        cleanCache(CacheNames.RUIS_UTILCACHE)
        cleanCache(CacheNames.FIND_PERMISSION)
    }
}